package com.xh.cdp.sys.dao.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.xh.cdp.common.BaseDao;
import com.xh.cdp.sys.dao.SysMenuDao;
import com.xh.cdp.sys.entity.SysMenuEntity;
import com.xh.cdp.sys.entity.temp.ComboTreeModel;

@Repository
public class SysMenuDaoImpl extends BaseDao<SysMenuEntity, String> implements SysMenuDao {

	@Override
	public List<SysMenuEntity> showLeftMenuTree(String menuPid, String roleId) {
		return listEntityByHql(
				"SELECT new SysMenuEntity(m.id, m.pid, m.text, m.url, m.state, m.enable) FROM SysMenuEntity m, SysRoleEntity r, SysRoleMenuEntity rm WHERE m.id = rm.sysMenuId AND rm.sysRoleId = r.id AND m.pid = ? AND r.id = ? AND m.enable = 1",
				menuPid, roleId);
	}

	@Override
	public List<SysMenuEntity> listMenus(String id) {
		if (id == null) {
			id = "0";
		}
		return listEntityByHql(
				"SELECT new SysMenuEntity(m.id, m.pid, m.text, m.url, m.state, m.enable) FROM SysMenuEntity m WHERE m.pid = ? AND  m.enable = 1",
				id);
	}

	@Override
	public List<SysMenuEntity> listParentMenus() {
		return listEntityByHql(
				"SELECT new SysMenuEntity(m.id, m.pid, m.text, m.url, m.state, m.enable) FROM SysMenuEntity m WHERE m.pid = 0");
	}

	@Override
	public String addMenu(SysMenuEntity sysMenuEntity) {
		sysMenuEntity.setPid(sysMenuEntity.getPid());
		sysMenuEntity.setText(sysMenuEntity.getText());
		sysMenuEntity.setUrl(sysMenuEntity.getUrl());

		// byte enable = ("on".equals(sysMenuEntity.getEnable())) ? (byte) 1 :
		// (byte) 2;
		// String state = ("on".equals(sysMenuEntity.getState())) ? "open" :
		// "closed";

		sysMenuEntity.setEnable(sysMenuEntity.getEnable());
		sysMenuEntity.setState(sysMenuEntity.getState());
		return save(sysMenuEntity);
	}

	@Override
	public List<Object> listMenuById(String id) {
		SysMenuEntity sysMenuEntity = uniqueEntityByHql(
				"SELECT new SysMenuEntity(m.id, m.pid, m.text, m.url, m.state, m.enable) FROM SysMenuEntity m WHERE m.id = ?",
				id);
		String parentMenuName = uniqueSinglePropertyByHql(String.class,
				"SELECT m.text FROM SysMenuEntity m WHERE m.id = ?",
				("0".equals(sysMenuEntity.getPid())) ? id : sysMenuEntity.getPid());
		List<Object> list = new ArrayList<>();
		list.add(sysMenuEntity);
		list.add(parentMenuName);

		System.out.println("list:" + list);
		return list;
	}

	@Override
	public void updateMenuById(SysMenuEntity sysMenuEntity, String id) {
		SysMenuEntity beforeUpdateMenu = get(id);
		if (beforeUpdateMenu != null) {
			System.out.println("beforeUpdateMenu:" + beforeUpdateMenu + "sysMenuEntity:" + sysMenuEntity);
			beforeUpdateMenu.setPid(sysMenuEntity.getPid());
			beforeUpdateMenu.setText(sysMenuEntity.getText());
			beforeUpdateMenu.setUrl(sysMenuEntity.getUrl());

			// byte enable = ("on".equals(sysMenuEntity.getState())) ? (byte) 1
			// : (byte) 2;
			// String state = ("on".equals(sysMenuEntity.getState())) ? "open" :
			// "closed";

			beforeUpdateMenu.setEnable(sysMenuEntity.getEnable());
			beforeUpdateMenu.setState(sysMenuEntity.getState());
		}
		update(beforeUpdateMenu);
	}

	@Override
	public Integer deleteMenuById(String id) {
		String hql = "UPDATE SysMenuEntity SET enable = 2 WHERE id = ? ";
		Integer row = getSession().createQuery(hql).setParameter(0, id).executeUpdate();
		return row;
	}

	public List<SysMenuEntity> ForGetMenuFor(Integer id) {
		List<SysMenuEntity> menuList = null;
		try {
			menuList = listEntityByHql("FROM SysMenuEntity WHERE pid = ?", 0);
		} catch (Exception e) {
			throw new RuntimeException();
		}
		System.out.println("menuList:" + menuList);
		return menuList;
	}

	@Override
	public List<ComboTreeModel> getPowerCombotree() {
		List<ComboTreeModel> list = new ArrayList<ComboTreeModel>();
		List<SysMenuEntity> fatherList = listEntityByHql("FROM SysMenuEntity WHERE pid = '0'");
		for (int i = 0; i < fatherList.size(); i++) {
			ComboTreeModel ctm = new ComboTreeModel();
			ctm.setId(fatherList.get(i).getId());
			ctm.setText(fatherList.get(i).getText());
			List<ComboTreeModel> children = new ArrayList<ComboTreeModel>();
			List<SysMenuEntity> childrenList = listEntityByHql("FROM SysMenuEntity WHERE pid = ?", ctm.getId());
			for (int j = 0; j < childrenList.size(); j++) {
				ComboTreeModel comboTreeModel = new ComboTreeModel();
				comboTreeModel.setId(childrenList.get(j).getId());
				comboTreeModel.setText(childrenList.get(j).getText());
				children.add(comboTreeModel);
			}
			ctm.setChildren(children);
			list.add(ctm);
		}
		return list;
	}

	@Override
	public List<ComboTreeModel> getPowerCombotree(List<String> ownMenuIds) {
		List<ComboTreeModel> list = new ArrayList<ComboTreeModel>();
		List<SysMenuEntity> fatherList = listEntityByHql("FROM SysMenuEntity WHERE pid = '0'");
		for (int i = 0; i < fatherList.size(); i++) {
			ComboTreeModel ctm = new ComboTreeModel();
			ctm.setId(fatherList.get(i).getId());
			ctm.setText(fatherList.get(i).getText());
			if (ownMenuIds.contains(ctm.getId())) {
				ctm.setCheck(true);
			}
			List<ComboTreeModel> children = new ArrayList<ComboTreeModel>();
			List<SysMenuEntity> childrenList = listEntityByHql("FROM SysMenuEntity WHERE pid = ?", ctm.getId());
			for (int j = 0; j < childrenList.size(); j++) {
				ComboTreeModel comboTreeModel = new ComboTreeModel();
				comboTreeModel.setId(childrenList.get(j).getId());
				comboTreeModel.setText(childrenList.get(j).getText());
				if (ownMenuIds.contains(comboTreeModel.getId())) {
					comboTreeModel.setCheck(true);
				}
				children.add(comboTreeModel);
			}
			ctm.setChildren(children);
			list.add(ctm);
		}
		return list;
	}

	@Override
	public Set<String> getFatherMenuIds(String rolePower) {
		Set<String> fatherMenuIds = new HashSet<>();
		String hql = "SELECT pid FROM SysMenuEntity WHERE id IN (:ids)";
		List<String> resultList = getSession().createQuery(hql).setParameterList("ids", rolePower.split(","))
				.getResultList();
		System.out.println(resultList);
		for (int i = 0; i < resultList.size(); i++) {
			if (!"0".equals(resultList.get(i))) {
				fatherMenuIds.add(resultList.get(i));
			}
		}
		System.out.println(fatherMenuIds);
		return fatherMenuIds;
	}
}
